<?php

namespace App\Http\Controllers;

use App\Http\Model\ProductModel;


use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;


class ProductController extends Controller
{
    public function test(Request $req)
    {

        var_dump(Carbon::now());

    }

    public function getProductList(Request $req)
    {

        $data = $this->getReqArray($req->getContent());

        if (!array_key_exists("show", $data)) {
            $show = 10;
        } else {
            $show = $data["show"];

            preg_match('/^\d+$/', $show, $match);

            $show = $match == 0 ? PAGE_NUM : $show;
        }

        $productModel = new ProductModel();

        $res = $productModel->getProductList($show);

        return response()->json($this->getData(SUCCESS_CODE, $res));
    }

    public function addProduct(Request $req)
    {
        $data = $this->getReqArray($req->getContent());

        $checkRes = $this->checkProduct($data);

        if ($checkRes != "success") return $this->getMsg(ERROR_CODE, $checkRes);

        $tags = $data["tags"];

        $product = $data["product"];

        $sku = $data["sku"];

        $pm = new ProductModel();

        $addRes = $pm->addProduct($product, $tags, $sku);

        $res = array();

        $res["code"] = $addRes == "success" ? SUCCESS_CODE : ERROR_CODE;

        $res["msg"] = $addRes == "success" ? "添加成功" : $addRes;

        return response()->json($res);
    }

    public function updateProduct(Request $req)
    {
        $data = $this->getReqArray($req->getContent());

        $checkRes = $this->checkProduct($data);

        if ($checkRes != "success") return $this->getMsg(ERROR_CODE, $checkRes);

        $id = $data['id'];

        $tag = $data['tags'];

        $sku = $data['sku'];

        $product = $data['product'];

        $pm = new ProductModel();

        $updateRes = $pm->updateProduct($id, $product, $tag, $sku);

        $res = array();

        $res["code"] = $updateRes == "success" ? SUCCESS_CODE : ERROR_CODE;

        $res["msg"] = $updateRes == "success" ? "更新成功" : $updateRes;

        return response()->json($res);
    }

    private function checkProduct($data)
    {

        $msg = $this->productValidator($data, "all", false);

        if ($msg != "success") return $msg;

        $tags = $data["tags"];

        $sku = $data["sku"];

        $msg = $this->productValidator($tags, "tags", true);
        if ($msg != "success") return $msg;

        $msg = $this->productValidator($sku, "sku", true);
        if ($msg != "success") return $msg;

        return "success";
    }

    public function delProduct(Request $req)
    {
        $data = $this->getReqArray($req->getContent());

        $validator = Validator::make($data, [
            "id" => "required|integer"
        ]);

        if ($validator->fails()) {
            return $validator->errors()->first();
        }

        $productModel = new ProductModel();

        $delRes = $productModel->delProduct($data['id']);

        $res = array();

        $res["code"] = $delRes == "success" ? 1 : 2;

        $res["msg"] = $delRes == "success" ? "删除成功" : $delRes;

        return response()->json($res);
    }


    public function selectProduct($id)
    {

        preg_match('/^\d+$/', $id, $match);

        if(empty($match)){
            return $this->getMsg(ERROR_CODE, "商品ID不正确");
        }

        $pm = new ProductModel();

        $selectRes = $pm->selectProduct($id);

        if (!empty($selectRes)) {
            return response()->json($this->getData(SUCCESS_CODE,$selectRes));
        }

        return response()->json($this->getMsg(ERROR_CODE,"没有该商品"));

    }

    private function productValidator($data, $key, $isArray = false)
    {
        $ruls = array();

        switch ($key) {
            case "all":
                $ruls = [
                    'product' => 'required',
                    'tags' => 'required',
                    'sku' => 'required',
                ];
                break;
            case "tags":
                $ruls = [
                    "tag_id" => "required|integer",
                    "value" => "required"
                ];
                break;
            case "sku":
                $ruls = [
                    "attr" => "required",
                    "original_price" => "required|numeric",
                    "price" => "required|numeric",
                    "quantity" => "required|integer",
                    "sort" => "required|integer|between:1,99"
                ];
                break;
            case "del":
                $ruls = [
                    "id" => "required|integer"
                ];
        }

        if ($isArray) {
            foreach ($data as $k => $v) {

                $validator = Validator::make((array)$v, $ruls);
                if ($validator->fails()) {
                    return $validator->errors()->first();
                }
            }
        } else {
            $validator = Validator::make($data, $ruls);

            if ($validator->fails()) {
                return $validator->errors()->first();
            }
        }

        return "success";
    }
}
